package com.mingtu.guards.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import androidx.recyclerview.widget.RecyclerView;

import com.mingtu.guards.bean.InfraredDequiBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class FliteDequAdapter extends RecyclerView.Adapter<FliteDequAdapter.ActivityHolder> {
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private List<InfraredDequiBean.DataBean.ListBean> mData;
    private HashMap<Integer, Boolean> hashMap = new HashMap<>(); //存放是否选中的状态，默认为false
    private HashMap<Integer, String> dequNameMap = new HashMap<>();


    public FliteDequAdapter(Context context, List<InfraredDequiBean.DataBean.ListBean> mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public FliteDequAdapter.ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(com.mingtu.center.R.layout.item_filte, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, final int position) {
        if (mData != null) {
            String cameraName = mData.get(position).getCameraName();
            holder.checkbox.setText(cameraName);
            holder.checkbox.setChecked(hashMap.get(position));

            final ActivityHolder finalHolder = holder;
            holder.checkbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String cameraName = mData.get(position).getCameraName() + "";
                    boolean isChecked = finalHolder.checkbox.isChecked();

                    if (isChecked) {
                        finalHolder.checkbox.setChecked(true);
                        dequNameMap.put(position, cameraName);

                    } else {
                        finalHolder.checkbox.setChecked(false);
                        dequNameMap.put(position, null);
                    }
                }
            });

        }
    }


    /**
     * 获取选中的deptId  list
     */
    public List<String> getDequNameList() {
        List<String> dequNameList = new ArrayList<>();
        for (int i = 0; i < dequNameMap.size(); i++) {
            String deptId = dequNameMap.get(i);
            if (deptId != null) {
                dequNameList.add(deptId);
            }
        }
        return dequNameList;
    }

    /**
     * 重置选中状态
     */
    public void resetData() {
        for (int i = 0; i < mData.size(); i++) {
            hashMap.put(i, false);
            dequNameMap.put(i, null);
        }
        notifyDataSetChanged();
    }

    /**
     * 更新数据
     */
    public void upData(List<InfraredDequiBean.DataBean.ListBean> data) {
        mData = data;
        notifyDataSetChanged();
        if (mData != null) {
            if (mData.size() > 0) {
                for (int i = 0; i < mData.size(); i++) {
                    hashMap.put(i, false);
                    dequNameMap.put(i, null);
                }
            }
        }
    }


    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View convertView;
        private final CheckBox checkbox;

        public ActivityHolder(View itemView) {
            super(itemView);
            convertView = itemView;
            checkbox = (CheckBox) convertView.findViewById(com.mingtu.center.R.id.checkbox);

        }
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position, boolean isChecked);
    }

}
